package com.petecat.interchan.base.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.alibaba.fastjson.JSONArray;
import com.petecat.interchan.base.project.entity.EsServer;
/**
 * Excel导出
 * @ClassName:  ExeclOutFile   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: ####
 * @date:   2018年2月27日 下午3:41:52   
 * @Copyright: 2018 www.tydic.com Inc. All rights reserved. 
 * 注意：本内容仅限于深圳天源迪科信息技术股份有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
public class ExeclOutFile {
	
	
	public static void main(String[] args) {
		
	}
	
	/**
     * 创建excel并填入数据
     * @param head 数据头
     * @param body 主体数据
     * @return HSSFWorkbook
     */
    public static HSSFWorkbook expExcel(HSSFWorkbook workbook,JSONArray head, List<JSONArray> body,String sheetName,Integer sheetNumber) {
        //创建一个sheet工作表
        HSSFSheet sheet = workbook.createSheet();
        //设置行宽
        sheet.setDefaultColumnWidth(28);
        //设置字体
        HSSFFont fontStyle = workbook.createFont();
        fontStyle.setFontName("宋体");
        fontStyle.setFontHeightInPoints((short)20);
        HSSFCellStyle style = workbook.createCellStyle();  
        style.setFont(fontStyle);
        workbook.setSheetName(sheetNumber, sheetName);
        //创建第0行表头，再在这行里在创建单元格，并赋值
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = null;
        for (int i = 0; i < head.size(); i++) {
            cell = row.createCell(i);
            cell.setCellValue(head.getString(i));//设置值
            cell.setCellStyle(style);
        }
        //将主体数据填入Excel中
        for (int i = 0, isize = body.size(); i < isize; i++) {
            row = sheet.createRow(i + 1);
            JSONArray json = body.get(i);
            for (int j = 0, jsize = json.size(); j < jsize; j++) {
            	cell = row.createCell(j);
                cell.setCellValue(json.getString(j));//设置值
            }
        }
        return workbook;
    }

	/**
     * 文件输出
     * @date 2018年2月27日 下午5:26:23
     * @param workbook 填充好的workbook
     * @param path 存放的位置
     */
    public static void outFile(HSSFWorkbook workbook,String path) {
        OutputStream os=null;
        try {
            os = new FileOutputStream(new File(path));
            workbook.write(os);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}
